<template>
    <div>
        <el-form ref="form" :model="obj.data" label-width="90px">
            <el-form-item label="标题" prop="title" :rules="[formRulesMixin.notNull]">
                <el-input v-model="obj.data.title" clearable autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="类别" prop="type" :rules="[formRulesMixin.notNull]">
                <el-select v-model="obj.data.type" placeholder="政策类别" class="search_select" clearable>
                    <el-option v-for="(item,index) in obj.alltype" :key="index" :label="item.name" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="发布时间" prop="sendtime" :rules="[formRulesMixin.notNull]">
                <DatePicker v-model:value="obj.data.sendtime" valueformat="YYYY-MM-DD hh:mm:ss" format="YYYY-MM-DD hh:mm:ss" type="datetime" />
            </el-form-item>
            <el-form-item label="政策内容" prop="msg" :rules="[formRulesMixin.notNull]">
                <Editer v-model:value="obj.data.msg" width="100%" height="60vh" :maxheight="100000" />
            </el-form-item>
            <el-form-item class="dialog-btns-item">
                <div class="dialog-btns">
                    <span class="btn clear" @click="$emit('close')">取消</span>
                    <span class="btn sub ml10" @click.prevent="upData">提交</span>
                </div>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup>
    import { reactive,ref,getCurrentInstance,toRaw,inject } from 'vue'
    import Editer from "@/components/editer.vue"
    import DatePicker from '@/components/date_picker.vue'
    let { proxy } = getCurrentInstance()
    
    let obj = reactive({
        data:{
            policy_id: inject("id").value || 0,
            title: "",
            msg: "",
            type:"",
            sendtime: proxy.$utils.dateFormat(new Date(), "yyyy-MM-dd hh:mm:ss"),
        },
    })
    
    let befortinit = async () => {
        obj.alltype = await proxy.$utils.getFinancialPolicyType(proxy)
    }
    befortinit()
    let init = async () => {
        if(obj.data.policy_id > 0){
            proxy.$ajax({
                url: 'policy/get',
                data: obj.data,
                bfLoading: true
            }).then(d => {
                if(d.success){
                    obj.data = {...obj.data, ...d.msg}
                }else{
                    proxy.elMessageMixin(d.err, 'error')
                }
            }).catch(() => { })
        }
    }
    init()
    let upData = () => {
        proxy.$refs['form'].validate(valid => {
            if (valid) {
                proxy.$ajax({
                    url: 'policy/design',
                    data: obj.data,
                    bfLoading: true,
                    // timeout:60000
                }).then(d => {
                    if(d.success){
                        proxy.elMessageMixin(`${obj.data.policy_id>0?'修改':"添加"}成功`, 'success')
                        proxy.$emit('init')
                        proxy.$emit('close')
                    }else{
                        proxy.elMessageMixin(d.err, 'error')
                    }
                }).catch(() => { })
            }else{
                return false
            }
        })
    }
</script>

<style scoped>
</style>
